package warrper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Rect implements Comparable<Rect>{//矩形
    private int width;
    private int height;
    public Rect(){
    }
    public Rect(int width, int height) {
        this.width = width;
        this.height=height;
    }

    /**
     * 对象进行比较时，底层会调用此方法
     * o1.compareTo(o2)
     * o1<o2 返回值小于0
     * o1=o2 返回值等于0
     * o1>o2 返回值大于0
     * @return
     */
    @Override
    public int compareTo(Rect o) {
        return this.width-o.width;
    }

    @Override
    public String toString() {
        return "Rect{" +
                "width=" + width +
                ", height=" + height +
                '}';
    }
}

public class CollectionSortDemo {
    public static void main(String[] args) {
        Rect p1=new Rect(10,20);
        Rect p2=new Rect(5,30);
        Rect p3=new Rect(8,30);
        List<Rect> list=new ArrayList<>();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        Collections.sort(list);//List集合中的元素需要实现Comparable接口
        System.out.println(list);
    }
}
